package com.lun.blog.controller;

import com.lun.blog.dto.BlogDto;
import com.lun.blog.dto.Count;
import com.lun.blog.exceptions.NotFundException;
import com.lun.blog.pojo.*;
import com.lun.blog.service.*;
import com.lun.blog.vo.BlogQuery;
import com.lun.blog.vo.SearchQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Autowired
    BlogService blogService;

    @Autowired
    UserService userService;

    @Autowired
    TypeService typeService;
    @Autowired
    CommentService commentService;

    @Autowired
    MessageService messageService;

    @GetMapping("/")
    public String index(Model model, HttpSession session){
        Blog blog = blogService.findBlogByCreateTime();
        User user = userService.findByUserId(blog.getUser().getId());
        Type type = typeService.findById(blog.getType().getId());
        BlogDto dto=new BlogDto();
        dto.setBlogId(blog.getId());
        dto.setTypeId(type.getId());
        dto.setTypeName(type.getName());
        dto.setCreateBy(user.getNickname());
        dto.setCreateTime(blog.getCreateTime());
        dto.setDescription(blog.getDescription());
        dto.setTitle(blog.getTitle());
        dto.setView(blog.getView());
        dto.setAddress("http://localhost:8080/");
        dto.setComment(500);
        dto.setPicture(blog.getFirstPicture());
        model.addAttribute("blogDto",dto);

        //博客底部数字统计
        Long countBlog = blogService.countBlog();
        Long view = blogService.countView();
        Long comment = commentService.countComment();
        Long message = messageService.countMessage();
        Count count = new Count();
        count.setBlog(countBlog);
        count.setComment(comment);
        count.setView(view);
        count.setMessage(message);
        session.setAttribute("count",count);
        return "index";
    }

    @GetMapping("/about")
    public String aboutMe(){
        return "about";
    }

    @GetMapping("/toMusic")
    public String toMusic(){
        return "music";
    }

    /**
     * 全局搜索接口
     * @param query
     * @return
     */
    @GetMapping("/search")
    public String search(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         SearchQuery query,
                         Model model){
        Page<Blog> blogs = blogService.likeByBlog(query, pageable);
        List<Blog> content = blogs.getContent();
        Set<Long> userId = content.stream().map(blog -> blog.getUser().getId()).collect(Collectors.toSet());
        Set<Long> typeId = content.stream().map(blog -> blog.getType().getId()).collect(Collectors.toSet());
        List<User> userList = userService.findByUserIds(userId);
        List<Type> typeList = typeService.findByTypeId(typeId);

        for (Blog blog : content) {
            for (User user : userList) {
                if(blog.getUser().getId().equals(user.getId())){
                    blog.setUserName(user.getNickname());
                    break;
                }
            }
            for (Type type : typeList) {
                if(blog.getType().getId().equals(type.getId())){
                    blog.setTypeName(type.getName());
                    break;
                }
            }
            List<Comment> comments = commentService.findByBlogId(blog.getId());
            if(!CollectionUtils.isEmpty(comments)){
                blog.setComment(comments.size());
            }
            blog.setAddress("http://localhost:8080/");
        }
        model.addAttribute("page",blogs);
        return "search";
    }

}
